//命名空间
var NMSChassis = {};
NMSChassis.BASEURL = "resources/chassisImgs";
NMSChassis.shelfFrame = function(opt) {
	// 配置对象
	this.option = {
		index : 0,
		moid : "",
		classname : "",
		devName : "",
		canvasId : "canvas",// 使用画布的ID 默认ID为canvas
		canvasWidth : 0,
		canvasHeight : 0,
		shelf : [],// 机架信息
		cards : [],// 读取的卡的信息
		ports : [],
		snmp : [],
		power : null
	// 端口的状态信息
	}
	var _thisChassis = this;

	var _canvas;
	var _g2d;
	this.nameToMenu = {};
	this._mouseEvent;
	this.shelfFlag = false;
	this.cardFlag = false;
	this.cardTypeFlag = false;
	this.portType = "";
	this.cardType = "";
	this.shelfType = "";
	this.shelfMenu = "";
	this.cardMenu = [];
	this.canvasId = opt.canvasId;
	this._animate;
	function _initOption(opt) {
		_thisChassis.option.index = opt.index ? opt.index : 0;
		_thisChassis.option.canvasId = opt.canvasId ? opt.canvasId : "canvas";
		_thisChassis.option.canvasWidth = opt.canvasWidth ? opt.canvasWidth : 0;
		_thisChassis.option.canvasHeight = opt.canvasHeight ? opt.canvasHeight
				: 0;
		_thisChassis.option.shelf = opt.shelf ? opt.shelf : [];
		_thisChassis.option.cards = opt.cards ? opt.cards : [];
		_thisChassis.option.ports = opt.ports ? opt.ports : [];
		_thisChassis.option.snmp = opt.snmp ? opt.snmp : [];
		_thisChassis.option.moid = opt.moid ? opt.moid : "";
		_thisChassis.option.classname = opt.classname ? opt.classname : "";
		_thisChassis.option.devName = opt.devName ? opt.devName : "";
		_thisChassis.option.power = opt.power ? opt.power : null;
	}
	function _initCanvas(option) {
		var id = option.canvasId;
		_canvas = document.getElementById(id[0] == "#" ? id.slice(1, id.length)
				: id); // 兼容#
		_g2d = _canvas.getContext("2d");
		_thisChassis.g2d = _g2d;
		_canvas.width = option.canvasWidth;
		_canvas.height = option.canvasHeight;

	}
	function _drawShelf() {
		var shelfImg = new Image();
		shelfImg.src = _thisChassis.option.shelf.Image.replace("images",
				NMSChassis.BASEURL);
		_g2d.drawImage(shelfImg, 0, 0);
		var tempSlot = [];
		var tempCard = [];
		var tempPort = [];
		if (_thisChassis.option.shelf.Slots.length) {// 不是单项
			tempSlot = _thisChassis.option.shelf.Slots;
		} else {
			tempSlot.push(_thisChassis.option.shelf.Slots);
		}
		if (_thisChassis.option.cards.length) {// 不是单项
			tempCard = _thisChassis.option.cards;
		} else {
			tempCard.push(_thisChassis.option.cards);
		}
		if (_thisChassis.option.ports.length) {// 不是单项
			tempPort = _thisChassis.option.ports;
		} else {
			tempPort.push(_thisChassis.option.ports);
		}
		for ( var i in tempSlot) {

			var slotImg = new Image();
			slotImg.src = tempSlot[i].Image.replace("images",
					NMSChassis.BASEURL);
			var sx = tempSlot[i].X;
			var sy = tempSlot[i].Y;
			var sw = tempSlot[i].W;
			var sh = tempSlot[i].H;
			_g2d.drawImage(slotImg, sx, sy, sw, sh);
			for ( var j in tempCard) {
				if (tempCard[j].slotNum == i) {
					_g2d.beginPath();
					if (tempCard[j].status == "2") {
						_g2d.fillStyle = "#00FF00";/* 绿色 */
					} else
						_g2d.fillStyle = "#FF0000";/* 红色 */
					_g2d.fillRect(sx, sy, sw, sh);/* 绘制一个矩形，前两个参数决定开始位置，后两个分别是矩形的宽和高 */
					_g2d.closePath();
					var cardImg = new Image();
					cardImg.src = tempCard[j].Image.replace("images",
							NMSChassis.BASEURL);
					_g2d.drawImage(cardImg, sx, sy, sw, sh);
					var tempCardPort = [];// card上的端口
					if (tempCard[j].Ports) {
						if (tempCard[j].Ports.length) {
							tempCardPort = tempCard[j].Ports;
						} else {
							tempCardPort.push(tempCard[j].Ports);
						}
						for ( var k in tempCardPort) {
							var portImg = new Image();
							portImg.src = tempCardPort[k].Image.replace(
									"images", NMSChassis.BASEURL);
							var px = tempCardPort[k].X;
							var py = tempCardPort[k].Y;
							var pw = tempCardPort[k].W;
							var ph = tempCardPort[k].H;
							_g2d.beginPath();
							_g2d.fillStyle = "#FF0000";
							_g2d.fillRect(parseInt(sx) + parseInt(px),
									parseInt(sy) + parseInt(py), pw, ph);/* 绘制一个矩形，前两个参数决定开始位置，后两个分别是矩形的宽和高 */
							_g2d.closePath();
							if (tempCardPort[k].PortNum == "-1") {
								_g2d.beginPath();
								_g2d.fillStyle = "#FFFFFF";/* 白色 */
								_g2d.fillRect(parseInt(sx) + parseInt(px),
										parseInt(sy) + parseInt(py), pw, ph);/* 绘制一个矩形，前两个参数决定开始位置，后两个分别是矩形的宽和高 */
								_g2d.closePath();
							} else {
								if (_thisChassis.option.classname == "EponONUDevice") {
									_g2d.beginPath();
									for ( var m in tempPort) {
										if(m==tempCardPort[k].PortNum ){
											if (tempPort[m] == "1") {
												_g2d.fillStyle = "#00FF00";/* 绿色 */
											}
											if (tempPort[m] == "2") {
												_g2d.fillStyle = "#FFFFFF";/* 白色 */
											}
											_g2d.fillRect(
															parseInt(sx) + parseInt(px),
															parseInt(sy) + parseInt(py),
															pw, ph);/* 绘制一个矩形，前两个参数决定开始位置，后两个分别是矩形的宽和高 */
											_g2d.closePath();
										}
									}
									
								} else if (_thisChassis.option.classname == "GponONUDevice") {
											_g2d.beginPath();
											if (tempPort[k] == "1") {
												_g2d.fillStyle = "#00FF00";/* 绿色 */
											}
											if (tempPort[k] == "2") {
												_g2d.fillStyle = "#FFFFFF";/* 白色 */
											}
											_g2d
													.fillRect(
															parseInt(sx)
																	+ parseInt(px),
															parseInt(sy)
																	+ parseInt(py),
															pw, ph);/* 绘制一个矩形，前两个参数决定开始位置，后两个分别是矩形的宽和高 */
											_g2d.closePath();
								} else {
									for ( var m in tempPort) {
										if (tempPort[m].slotNumber == i
												&& tempPort[m].portNumber == tempCardPort[k].PortNum) {
											_g2d.beginPath();
											if (tempPort[m].ifAdminStatus == "1") {
												if (tempPort[m].ifOperStatus == "1")
													_g2d.fillStyle = "#00FF00";/* 绿色 */
												else
													_g2d.fillStyle = "#FFFFFF";/* 白色 */
											} else
												_g2d.fillStyle = "#FF0000";/* 红色 */
											_g2d
													.fillRect(
															parseInt(sx)
																	+ parseInt(px),
															parseInt(sy)
																	+ parseInt(py),
															pw, ph);/* 绘制一个矩形，前两个参数决定开始位置，后两个分别是矩形的宽和高 */
											_g2d.closePath();
										}
									}
								}

							}

							_g2d.drawImage(portImg,
									parseInt(sx) + parseInt(px), parseInt(sy)
											+ parseInt(py), pw, ph);
						}
					}
					if (tempCard[j].Led) {
						var tempCardLed = [];// card上的led
						if (tempCard[j].Led.length) {
							tempCardLed = tempCard[j].Led;
						} else {
							tempCardLed.push(tempCard[j].Led);
						}
						for ( var k in tempCardLed) {
							var lx = tempCardLed[k].X;
							var ly = tempCardLed[k].Y;
							var lw = tempCardLed[k].W;
							var lh = tempCardLed[k].H;
							if (tempCardLed[k].LedType == "port"
									|| tempCardLed[k].LedType == "poe") {// 对应port的灯
								_g2d.beginPath();
								_g2d.fillStyle = "#FF0000";/* 红色 */
								_g2d.arc(parseInt(sx) + parseInt(lx) + lw / 2,
										parseInt(sy) + parseInt(ly) + lw / 2,
										lw / 2, 0, 2 * Math.PI, true);
								_g2d.fill();
								_g2d.closePath();
								for ( var m in tempPort) {
									if (tempPort[m].slotNumber == i
											&& tempPort[m].portNumber == tempCardLed[k].LedNum) {
										_g2d.beginPath();
										if (tempPort[m].ifAdminStatus == "1") {
											if (tempPort[m].ifOperStatus == "1")
												_g2d.fillStyle = "#00FF00";/* 绿色 */
											else
												_g2d.fillStyle = "#FFFFFF";/* 白色 */
										} else
											_g2d.fillStyle = "#FF0000";/* 红色 */
										_g2d.arc(parseInt(sx) + parseInt(lx)
												+ lw / 2, parseInt(sy)
												+ parseInt(ly) + lw / 2,
												lw / 2, 0, 2 * Math.PI, true);
										_g2d.fill();
										_g2d.closePath();
									}
								}
							} else if (tempCardLed[k].LedType == "slot") {//对应port的灯
								for ( var m in tempPort) {
									if (tempPort[m].slotNumber == tempCardLed[k].LedNum) {
										_g2d.beginPath();
										_g2d.fillStyle = "#00FF00";/* 绿色 */
										_g2d.arc(parseInt(sx) + parseInt(lx)+lw/2,
												parseInt(sy) + parseInt(ly)+lw/2,
												lw/2, 0, 2 * Math.PI, true);
										_g2d.fill();
										_g2d.closePath();
									}
								}
							}else {// 其他灯
//								_g2d.beginPath();
//								_g2d.fillStyle = "#00FF00";/* 绿色 */
//								_g2d.arc(parseInt(sx) + parseInt(lx) + lw / 2,
//										parseInt(sy) + parseInt(ly) + lw / 2,
//										lw / 2, 0, 2 * Math.PI, true);
//								_g2d.fill();
//								_g2d.closePath();
							}

						}
					}// 绘制led
				}

			}
		}

		if (_thisChassis.option.shelf.PIcons) {
			var status = _thisChassis.option.power;
			var tempShelfPIcons = [];
			if (_thisChassis.option.shelf.PIcons.length) {// 不是单项
				tempShelfPIcons = _thisChassis.option.shelf.PIcons;
			} else {
				tempShelfPIcons.push(_thisChassis.option.shelf.PIcons);
			}
			if (status != "NULL" && status != null) {
				var pl = _thisChassis.option.shelf.PIcons.length;
				if (pl == 2) {
					var pIconImg0 = new Image();
					pIconImg0.src = tempShelfPIcons[0].Image.replace("images",
							NMSChassis.BASEURL);
					var ix0 = tempShelfPIcons[0].X;
					var iy0 = tempShelfPIcons[0].Y;
					var iw0 = tempShelfPIcons[0].W;
					var ih0 = tempShelfPIcons[0].H;
					var pIconImg1 = new Image();
					pIconImg1.src = tempShelfPIcons[1].Image.replace("images",
							NMSChassis.BASEURL);
					var ix1 = tempShelfPIcons[1].X;
					var iy1 = tempShelfPIcons[1].Y;
					var iw1 = tempShelfPIcons[1].W;
					var ih1 = tempShelfPIcons[1].H;

					if ("1" == status) {
						_g2d.beginPath();
						_g2d.fillStyle = "#00FF00";/* 绿色 */
						_g2d.fillRect(ix0, iy0, iw0, ih0);
						_g2d.closePath();
						_g2d.drawImage(pIconImg0, ix0, iy0, iw0, ih0);
						_g2d.beginPath();
						_g2d.fillStyle = "#FF0000";/* 红色 */
						_g2d.fillRect(ix1, iy1, iw1, ih1);
						_g2d.closePath();
						_g2d.drawImage(pIconImg1, ix1, iy1, iw1, ih1);
					}
					if ("2" == status) {
						_g2d.beginPath();
						_g2d.fillStyle = "#FF0000";/* 红色 */
						_g2d.fillRect(ix0, iy0, iw0, ih0);
						_g2d.closePath();
						_g2d.drawImage(pIconImg0, ix0, iy0, iw0, ih0);
						_g2d.beginPath();
						_g2d.fillStyle = "#00FF00";/* 绿色 */
						_g2d.fillRect(ix1, iy1, iw1, ih1);
						_g2d.closePath();
						_g2d.drawImage(pIconImg1, ix1, iy1, iw1, ih1);
					}
					if ("3" == status) {
						_g2d.beginPath();
						_g2d.fillStyle = "#00FF00";/* 绿色 */
						_g2d.fillRect(ix0, iy0, iw0, ih0);
						_g2d.closePath();
						_g2d.drawImage(pIconImg0, ix0, iy0, iw0, ih0);
						_g2d.beginPath();
						_g2d.fillStyle = "#00FF00";/* 绿色 */
						_g2d.fillRect(ix1, iy1, iw1, ih1);
						_g2d.closePath();
						_g2d.drawImage(pIconImg1, ix1, iy1, iw1, ih1);
					}
					if ("4" == status) {
						_g2d.beginPath();
						_g2d.fillStyle = "#E6EE29";/* 黄色 */
						_g2d.fillRect(ix0, iy0, iw0, ih0);
						_g2d.closePath();
						_g2d.drawImage(pIconImg0, ix0, iy0, iw0, ih0);
						_g2d.beginPath();
						_g2d.fillStyle = "#E6EE29";/* 黄色 */
						_g2d.fillRect(ix1, iy1, iw1, ih1);
						_g2d.closePath();
						_g2d.drawImage(pIconImg1, ix1, iy1, iw1, ih1);
					}
				} else {
					var pIconImg0 = new Image();
					pIconImg0.src = tempShelfPIcons[0].Image.replace("images",
							NMSChassis.BASEURL);
					var ix0 = tempShelfPIcons[0].X;
					var iy0 = tempShelfPIcons[0].Y;
					var iw0 = tempShelfPIcons[0].W;
					var ih0 = tempShelfPIcons[0].H;
					if ("1" == status) {
						_g2d.beginPath();
						_g2d.fillStyle = "#00FF00";/* 绿色 */
						_g2d.fillRect(ix0, iy0, iw0, ih0);
						_g2d.closePath();
						_g2d.drawImage(pIconImg0, ix0, iy0, iw0, ih0);
					} else {
						_g2d.beginPath();
						_g2d.fillStyle = "#FF0000";/* 红色 */
						_g2d.fillRect(ix0, iy0, iw0, ih0);
						_g2d.closePath();
						_g2d.drawImage(pIconImg0, ix0, iy0, iw0, ih0);
					}
				}
			} else {
				for ( var i in tempShelfPIcons) {
					var pIconImg = new Image();
					pIconImg.src = tempShelfPIcons[i].Image.replace("images",
							NMSChassis.BASEURL);
//					console.log(pIconImg);
					var ix = tempShelfPIcons[i].X;
					var iy = tempShelfPIcons[i].Y;
					var iw = tempShelfPIcons[i].W;
					var ih = tempShelfPIcons[i].H;
					_g2d.drawImage(pIconImg, ix, iy, iw, ih);
				}
			}
		}

	}
	
	function _startAnimate(){

		_thisChassis._animate=setInterval(
				function(){
					_drawShelf();
					//console.log("Animate repaint()...");
				} , (1000/50) );
	}
	// 加载图片
	function _loadImg() {
		// 需要加载的图片数量
		var numImages = 0;
		// 已经加载的图片
		var loadedImages = 0;
		// 记录需要加载的路径
		var imgPaths = [];

		// 找到需要加载的图片
		if (_thisChassis.option.shelf.Image) {
			imgPaths[numImages] = _thisChassis.option.shelf.Image;
			numImages++;
		}
		// 找到需要加载的图片
		var tempShelf = [];
		if (_thisChassis.option.shelf.Slots) {
			if (_thisChassis.option.shelf.Slots.length) {// 不是单项
				tempShelf = _thisChassis.option.shelf.Slots;
			} else {
				tempShelf.push(_thisChassis.option.shelf.Slots);
			}
			for ( var tmpIndex1 in tempShelf) {
				if (tempShelf[tmpIndex1].Image) {
					if (_thisChassis.option.shelf.Image != "") {
						imgPaths[numImages] = tempShelf[tmpIndex1].Image;
						numImages++;
					}
				}
			}
		}
		if (_thisChassis.option.cards) {
			var tempCard = [];
			if (_thisChassis.option.cards.length) {// 不是单项
				tempCard = _thisChassis.option.cards;
			} else {
				tempCard.push(_thisChassis.option.cards);
			}
			for ( var tmpIndex2 in tempCard) {
				if (tempCard[tmpIndex2].Image) {
					imgPaths[numImages] = tempCard[tmpIndex2].Image;
					numImages++;
					var tempPort = [];
					if (tempCard[tmpIndex2].Ports) {
						if (tempCard[tmpIndex2].Ports.length) {// 不是单项
							tempPort = tempCard[tmpIndex2].Ports;
						} else {
							tempPort.push(tempCard[tmpIndex2].Ports);
						}
						for ( var tmpIndex3 in tempPort) {
							imgPaths[numImages] = tempPort[tmpIndex3].Image;
							numImages++;
						}
					}
				}
			}
		}

		// 开始加载
		for (var i = 0; i < numImages; i++) {
			var tmpImg = new Image();
			tmpImg.src = imgPaths[i].replace("images", NMSChassis.BASEURL);
			tmpImg.onload = function() {
				// 进度显示
				++loadedImages;

				// 若加载完成 开始执行回调
				if (loadedImages >= numImages) {

					_loadShelfMenu();
					_loadCardMenu();
					_startAnimate();
//					_drawShelf();
				}
			}
		}
	}
	this.updateOption = function(width,height){
		_thisChassis.option.canvasWidth = width;
		_thisChassis.option.canvasHeight = height;
	}
	this.initAndShow = function() {
		_g2d.clearRect(0, 0, _thisChassis.option.canvasWidth,
				_thisChassis.option.canvasHeight);
		_loadImg();
		_addMouseListener();
		_startPollingCard();// 开启卡的轮询
		_startPollingPort();// 开启端口轮询
	}
	this.repaintChassisWithEvent = function(e, x, y) {// 根据事件重绘
		_g2d.clearRect(0, 0, _thisChassis.option.canvasWidth,
				_thisChassis.option.canvasHeight);
		_renderChassis(_g2d, x, y, _thisChassis);
	}
	function _renderChassis(g2d, x, y, chassis) {

		var shelfImg = new Image();
		shelfImg.src = _thisChassis.option.shelf.Image.replace("images",
				NMSChassis.BASEURL);
		_g2d.drawImage(shelfImg, 0, 0);
		var tempSlot = [];
		var tempCard = [];
		var tempPort = [];
		if (_thisChassis.option.shelf.Slots.length) {// 不是单项
			tempSlot = _thisChassis.option.shelf.Slots;
		} else {
			tempSlot.push(_thisChassis.option.shelf.Slots);
		}
		if (_thisChassis.option.cards.length) {// 不是单项
			tempCard = _thisChassis.option.cards;
		} else {
			tempCard.push(_thisChassis.option.cards);
		}
		if (_thisChassis.option.ports.length) {// 不是单项
			tempPort = _thisChassis.option.ports;
		} else {
			tempPort.push(_thisChassis.option.ports);
		}
		for ( var i in tempSlot) {
			var slotImg = new Image();
			slotImg.src = tempSlot[i].Image.replace("images",
					NMSChassis.BASEURL);
			var sx = tempSlot[i].X;
			var sy = tempSlot[i].Y;
			var sw = tempSlot[i].W;
			var sh = tempSlot[i].H;
			_g2d.drawImage(slotImg, sx, sy, sw, sh);
			_g2d.rect(sx, sy, sw, sh);
			if (chassis.shelfFlag == false){
				chassis.shelfFlag = (x && y && g2d.isPointInPath(x, y));
				if (chassis.shelfFlag == true) {
					_thisChassis.slotNum = i;
				}
			}
			for ( var j in tempCard) {
				if (tempCard[j].slotNum == i) {
					_g2d.beginPath();
					if (tempCard[j].status == "2") {
						_g2d.fillStyle = "#00FF00";/* 绿色 */
					} else
						_g2d.fillStyle = "#FF0000";/* 红色 */
					_g2d.fillRect(sx, sy, sw, sh);/* 绘制一个矩形，前两个参数决定开始位置，后两个分别是矩形的宽和高 */
					_g2d.rect(sx, sy, sw, sh);
					if (chassis.cardTypeFlag == false) {
						chassis.cardTypeFlag = (x && y && g2d.isPointInPath(x, y));
						if (chassis.cardTypeFlag == true) 
							chassis.shelfType = tempCard[j].CardType;
					}
					_g2d.closePath();
					
					var cardImg = new Image();
					cardImg.src = tempCard[j].Image.replace("images",
							NMSChassis.BASEURL);
					_g2d.drawImage(cardImg, sx, sy, sw, sh);
					var tempCardPort = [];
					if (tempCard[j].Ports) {
						if (tempCard[j].Ports.length) {// 不是单项
							tempCardPort = tempCard[j].Ports;
						} else {
							tempCardPort.push(tempCard[j].Ports);
						}
						for ( var k in tempCardPort) {
							var portImg = new Image();
							portImg.src = tempCardPort[k].Image.replace(
									"images", NMSChassis.BASEURL);
							var px = tempCardPort[k].X;
							var py = tempCardPort[k].Y;
							var pw = tempCardPort[k].W;
							var ph = tempCardPort[k].H;
							_g2d.beginPath();
							_g2d.fillStyle = "#FF0000";
							_g2d.fillRect(parseInt(sx) + parseInt(px),
									parseInt(sy) + parseInt(py), pw, ph);/* 绘制一个矩形，前两个参数决定开始位置，后两个分别是矩形的宽和高 */
							_g2d.closePath();
							if (tempCardPort[k].PortNum == "-1") {
								_g2d.beginPath();
								_g2d.fillStyle = "#FFFFFF";/* 白色 */
								_g2d.fillRect(parseInt(sx) + parseInt(px),
										parseInt(sy) + parseInt(py), pw, ph);/* 绘制一个矩形，前两个参数决定开始位置，后两个分别是矩形的宽和高 */
								_g2d.closePath();
							} else {
								if (_thisChassis.option.classname == "EponONUDevice") {
									_g2d.beginPath();
									for ( var m in tempPort) {
										if(m==tempCardPort[k].PortNum ){
											if (tempPort[m] == "1") {
												_g2d.fillStyle = "#00FF00";/* 绿色 */
											}
											if (tempPort[m] == "2") {
												_g2d.fillStyle = "#FFFFFF";/* 白色 */
											}
											_g2d.fillRect(
															parseInt(sx) + parseInt(px),
															parseInt(sy) + parseInt(py),
															pw, ph);/* 绘制一个矩形，前两个参数决定开始位置，后两个分别是矩形的宽和高 */
											_g2d.closePath();
										}
									}
									
								} else if (_thisChassis.option.classname == "GponONUDevice") {
									for ( var m in tempPort) {
										if (parseInt(m) + 1 == tempCardPort[k].PortNum) {
											_g2d.beginPath();
											if (tempPort[k] == "1") {
												_g2d.fillStyle = "#00FF00";/* 绿色 */
											}
											if (tempPort[k] == "2") {
												_g2d.fillStyle = "#FFFFFF";/* 白色 */
											}
											_g2d
													.fillRect(
															parseInt(sx)
																	+ parseInt(px),
															parseInt(sy)
																	+ parseInt(py),
															pw, ph);/* 绘制一个矩形，前两个参数决定开始位置，后两个分别是矩形的宽和高 */
											_g2d.closePath();
										}
									}
								} else {
									for ( var m in tempPort) {

										if (tempPort[m].slotNumber == i// 槽位号相同
												&& tempPort[m].portNumber == tempCardPort[k].PortNum) {// 端口号相同
											_g2d.beginPath();
											if (tempPort[m].ifAdminStatus == "1") {
												if (tempPort[m].ifOperStatus == "1")
													_g2d.fillStyle = "#00FF00";/* 绿色 */
												else
													_g2d.fillStyle = "#FFFFFF";/* 白色 */
											} else
												_g2d.fillStyle = "#FF0000";/* 红色 */
											_g2d
													.fillRect(
															parseInt(sx)
																	+ parseInt(px),
															parseInt(sy)
																	+ parseInt(py),
															pw, ph);/* 绘制一个矩形，前两个参数决定开始位置，后两个分别是矩形的宽和高 */
											_g2d.closePath();
										}
									}
								}
							}

							_g2d.drawImage(portImg,
									parseInt(sx) + parseInt(px), parseInt(sy)
											+ parseInt(py), pw, ph);
							_g2d.rect(parseInt(sx) + parseInt(px), parseInt(sy)
									+ parseInt(py), pw, ph);

							if (chassis.cardFlag == false) {
								chassis.cardFlag = (x && y && g2d.isPointInPath(x, y));
								if (chassis.cardFlag == true) {
									chassis.portType = tempCardPort[k].PortType;
									chassis.cardType = tempCard[j].CardType;
									if (tempCardPort[k].PortNum == -1)
										_thisChassis.portStatus = 1;
									for ( var m in tempPort) {

										if (_thisChassis.option.classname == "EponONUDevice"||_thisChassis.option.classname == "GponONUDevice") {
											if (m== tempCardPort[k].PortNum) {// 端口号相同
												_thisChassis.portIfIndex = parseInt(m)+1;
												_thisChassis.portStatus = tempPort[m];
												_thisChassis.portNum ="PortNum:"+(parseInt(m)+1);
											}
										} else {
											if (tempPort[m].slotNumber == i// 槽位号相同
													&& tempPort[m].portNumber == tempCardPort[k].PortNum) {// 端口号相同
												_thisChassis.portIfIndex = tempPort[m].ifIndex;
												_thisChassis.portStatus = tempPort[m].ifAdminStatus;
												_thisChassis.portNum = tempPort[m].ifDescr;
											}
										}
									}

								}
							}
						}
					}
					if (tempCard[j].Led) {
						var tempCardLed = [];// card上的led
						if (tempCard[j].Led.length) {
							tempCardLed = tempCard[j].Led;
						} else {
							tempCardLed.push(tempCard[j].Led);
						}
						for ( var k in tempCardLed) {
							var lx = tempCardLed[k].X;
							var ly = tempCardLed[k].Y;
							var lw = tempCardLed[k].W;
							var lh = tempCardLed[k].H;
							if (tempCardLed[k].LedType == "port"
									|| tempCardLed[k].LedType == "poe") {// 对应port的灯
								_g2d.beginPath();
								_g2d.fillStyle = "#FF0000";/* 红色 */
								_g2d.arc(parseInt(sx) + parseInt(lx) + lw / 2,
										parseInt(sy) + parseInt(ly) + lw / 2,
										lw / 2, 0, 2 * Math.PI, true);
								_g2d.fill();
								_g2d.closePath();
								for ( var m in tempPort) {
									if (tempPort[m].slotNumber == i
											&& tempPort[m].portNumber == tempCardLed[k].LedNum) {
										_g2d.beginPath();
										if (tempPort[m].ifAdminStatus == "1") {
											if (tempPort[m].ifOperStatus == "1")
												_g2d.fillStyle = "#00FF00";/* 绿色 */
											else
												_g2d.fillStyle = "#FFFFFF";/* 白色 */
										} else
											_g2d.fillStyle = "#FF0000";/* 红色 */
										_g2d.arc(parseInt(sx) + parseInt(lx)
												+ lw / 2, parseInt(sy)
												+ parseInt(ly) + lw / 2,
												lw / 2, 0, 2 * Math.PI, true);
										_g2d.fill();
										_g2d.closePath();
									}
								}
							}else if (tempCardLed[k].LedType == "slot") {//对应port的灯
								for ( var m in tempPort) {
									if (tempPort[m].slotNumber == tempCardLed[k].LedNum) {
										_g2d.beginPath();
										_g2d.fillStyle = "#00FF00";/* 绿色 */
										_g2d.arc(parseInt(sx) + parseInt(lx)+lw/2,
												parseInt(sy) + parseInt(ly)+lw/2,
												lw/2, 0, 2 * Math.PI, true);
										_g2d.fill();
										_g2d.closePath();
									}
								}
							} else {
//								_g2d.beginPath();
//								_g2d.fillStyle = "#00FF00";/* 绿色 */
//								_g2d.arc(parseInt(sx) + parseInt(lx) + lw / 2,
//										parseInt(sy) + parseInt(ly) + lw / 2,
//										lw / 2, 0, 2 * Math.PI, true);
//								_g2d.fill();
//								_g2d.closePath();
							}

						}
					}// 绘制led

				}

			}
		}

		if (_thisChassis.option.shelf.PIcons) {
			var status = _thisChassis.option.power;
			var tempShelfPIcons = [];
			if (_thisChassis.option.shelf.PIcons.length) {// 不是单项
				tempShelfPIcons = _thisChassis.option.shelf.PIcons;
			} else {
				tempShelfPIcons.push(_thisChassis.option.shelf.PIcons);
			}

			if (status != "NULL" && status != null) {
				var pl = _thisChassis.option.shelf.PIcons.length;
				if (pl == 2) {
					var pIconImg0 = new Image();
					pIconImg0.src = tempShelfPIcons[0].Image.replace("images",
							NMSChassis.BASEURL);
					var ix0 = tempShelfPIcons[0].X;
					var iy0 = tempShelfPIcons[0].Y;
					var iw0 = tempShelfPIcons[0].W;
					var ih0 = tempShelfPIcons[0].H;
					var pIconImg1 = new Image();
					pIconImg1.src = tempShelfPIcons[1].Image.replace("images",
							NMSChassis.BASEURL);
					var ix1 = tempShelfPIcons[1].X;
					var iy1 = tempShelfPIcons[1].Y;
					var iw1 = tempShelfPIcons[1].W;
					var ih1 = tempShelfPIcons[1].H;

					if ("1" == status) {
						_g2d.beginPath();
						_g2d.fillStyle = "#00FF00";/* 绿色 */
						_g2d.fillRect(ix0, iy0, iw0, ih0);
						_g2d.closePath();
						_g2d.drawImage(pIconImg0, ix0, iy0, iw0, ih0);
						_g2d.beginPath();
						_g2d.fillStyle = "#FF0000";/* 红色 */
						_g2d.fillRect(ix1, iy1, iw1, ih1);
						_g2d.closePath();
						_g2d.drawImage(pIconImg1, ix1, iy1, iw1, ih1);
					}
					if ("2" == status) {
						_g2d.beginPath();
						_g2d.fillStyle = "#FF0000";/* 红色 */
						_g2d.fillRect(ix0, iy0, iw0, ih0);
						_g2d.closePath();
						_g2d.drawImage(pIconImg0, ix0, iy0, iw0, ih0);
						_g2d.beginPath();
						_g2d.fillStyle = "#00FF00";/* 绿色 */
						_g2d.fillRect(ix1, iy1, iw1, ih1);
						_g2d.closePath();
						_g2d.drawImage(pIconImg1, ix1, iy1, iw1, ih1);
					}
					if ("3" == status) {
						_g2d.beginPath();
						_g2d.fillStyle = "#00FF00";/* 绿色 */
						_g2d.fillRect(ix0, iy0, iw0, ih0);
						_g2d.closePath();
						_g2d.drawImage(pIconImg0, ix0, iy0, iw0, ih0);
						_g2d.beginPath();
						_g2d.fillStyle = "#00FF00";/* 绿色 */
						_g2d.fillRect(ix1, iy1, iw1, ih1);
						_g2d.closePath();
						_g2d.drawImage(pIconImg1, ix1, iy1, iw1, ih1);
					}
					if ("4" == status) {
						_g2d.beginPath();
						_g2d.fillStyle = "#E6EE29";/* 黄色 */
						_g2d.fillRect(ix0, iy0, iw0, ih0);
						_g2d.closePath();
						_g2d.drawImage(pIconImg0, ix0, iy0, iw0, ih0);
						_g2d.beginPath();
						_g2d.fillStyle = "#E6EE29";/* 黄色 */
						_g2d.fillRect(ix1, iy1, iw1, ih1);
						_g2d.closePath();
						_g2d.drawImage(pIconImg1, ix1, iy1, iw1, ih1);
					}
				} else {
					var pIconImg0 = new Image();
					pIconImg0.src = tempShelfPIcons[0].Image.replace("images",
							NMSChassis.BASEURL);
					var ix0 = tempShelfPIcons[0].X;
					var iy0 = tempShelfPIcons[0].Y;
					var iw0 = tempShelfPIcons[0].W;
					var ih0 = tempShelfPIcons[0].H;
					if ("1" == status) {
						_g2d.beginPath();
						_g2d.fillStyle = "#00FF00";/* 绿色 */
						_g2d.fillRect(ix0, iy0, iw0, ih0);
						_g2d.closePath();
						_g2d.drawImage(pIconImg0, ix0, iy0, iw0, ih0);
					} else {
						_g2d.beginPath();
						_g2d.fillStyle = "#FF0000";/* 红色 */
						_g2d.fillRect(ix0, iy0, iw0, ih0);
						_g2d.closePath();
						_g2d.drawImage(pIconImg0, ix0, iy0, iw0, ih0);
					}
				}
			} else {
				for ( var i in tempShelfPIcons) {
					var pIconImg = new Image();
					pIconImg.src = tempShelfPIcons[i].Image.replace("images",
							NMSChassis.BASEURL);
					var ix = tempShelfPIcons[i].X;
					var iy = tempShelfPIcons[i].Y;
					var iw = tempShelfPIcons[i].W;
					var ih = tempShelfPIcons[i].H;
					_g2d.drawImage(pIconImg, ix, iy, iw, ih);
				}
			}
		}
	}
	function _addMouseListener() {
		_thisChassis._mouseEvent = new NMSChassis.MEvent(_canvas, _thisChassis);
	}
	function _loadShelfMenu() {
		if (_thisChassis.option.shelf.ShelfMenus) {
			var tempShelfMenu = [];
			if (_thisChassis.option.shelf.ShelfMenus.length)
				tempShelfMenu = _thisChassis.option.shelf.ShelfMenus;
			else
				tempShelfMenu.push(_thisChassis.option.shelf.ShelfMenus);
			_thisChassis.shelfMenu = "";
			_thisChassis.shelfMenu = _thisChassis.shelfMenu + "[[";
			for ( var i in tempShelfMenu) {
				_thisChassis.shelfMenu = _thisChassis.shelfMenu + "{text :'"
						+ chassisNmsi18n[tempShelfMenu[i].Name] + "',"
						+ "func: function() { console.log('"
						+ tempShelfMenu[i].Classname + "')}},"

			}
			_thisChassis.shelfMenu = _thisChassis.shelfMenu + "]]";
			_thisChassis.nameToMenu[opt.shelf.Title] = _thisChassis.shelfMenu;
		}
		// }

	}
	function _loadCardMenu() {
		var tempcard = [];
		if (_thisChassis.option.cards.length)
			tempcard = _thisChassis.option.cards;
		else
			tempcard.push(_thisChassis.option.cards);
		for ( var k in tempcard) {
			if (tempcard[k].MenuGroups) {
				var tempMenuGroups = [];
				if (tempcard[k].MenuGroups.length)
					tempMenuGroups = tempcard[k].MenuGroups;
				else
					tempMenuGroups.push(tempcard[k].MenuGroups);
				for ( var i in tempMenuGroups) {
					if (tempMenuGroups[i].MenuIterm) {
						var tempMenuIterm = [];
						if (tempMenuGroups[i].MenuIterm.length)
							tempMenuIterm = tempMenuGroups[i].MenuIterm;
						else
							tempMenuIterm.push(tempMenuGroups[i].MenuIterm);
						var tempMenu = "";
						tempMenu = tempMenu + "[[";
						for ( var j in tempMenuIterm) {
							tempMenu = tempMenu + "{text :'"
									+ chassisNmsi18n[tempMenuIterm[j].Name]
									+ "'," + "func: function() {"
									+ tempMenuIterm[j].Classname + "}},"
						}
						tempMenu = tempMenu + "]]";
						// _thisChassis.cardMenu.push(tempMenu);
						var tempName = tempcard[k].CardType
								+ tempMenuGroups[i].PortType;
						_thisChassis.nameToMenu[tempName] = tempMenu;
					}
				}
			}
		}
		var blankMenu = "[["+ "{text :'"+ chassisNmsi18n["MenuCardEnable"]
		+ "'," + "func: function() {setMenuCardEnable()}}," +
				"{text :'"+ chassisNmsi18n["MenuCardDisable"]
		+ "'," + "func: function() {setMenuCardDisable()}}]]";
		_thisChassis.nameToMenu["blankMenu"] = blankMenu;
	}
	function _startPollingCard() {
		if (_thisChassis.option.shelf.IsPolling) {// 是否有IsPolling
			if (_thisChassis.option.shelf.IsPolling == "true") {// 需要轮询卡
				pollingCard();
				var cardPollingIndex = setInterval(pollingCard,
						_thisChassis.option.shelf.CardPollingTime);
				NMSChassis.cardPollingIndex[_thisChassis.option.index] = cardPollingIndex;
			}
		}
	}
	function _startPollingPort() {

		pollingPort();
		var portPollingIndex = setInterval(pollingPort,
				_thisChassis.option.shelf.PortPollingTime);
		NMSChassis.portPollingIndex[_thisChassis.option.index] = portPollingIndex;
	}
	function pollingPort() {
//		console.log("pollingPort");
		var hasPower = false;
		if (_thisChassis.option.shelf.PIcons) {
			hasPower = true;
		}
		var moid = _thisChassis.option.moid;
		var classname = _thisChassis.option.classname;
		var portType = [];
		if (_thisChassis.option.classname == "EponONUDevice") {
			if (_thisChassis.option.cards[0].Ports.length)
				tempPort = _thisChassis.option.cards[0].Ports;
			else
				tempPort.push(_thisChassis.option.cards[0].Ports);
			for ( var i in tempPort) {
				portType.push(tempPort[i].PortType);
			}

		}
		if (_thisChassis.option.classname == "GponONUDevice") {
			if (_thisChassis.option.cards[0].Ports.length)
				tempPort = _thisChassis.option.cards[0].Ports;
			else
				tempPort.push(_thisChassis.option.cards[0].Ports);
			for ( var i in tempPort) {
				portType.push(tempPort[i].PortType);
			}

		}

		var devname = _thisChassis.option.devName;
		var canvasId = _thisChassis.option.canvasId;
		var url = "topoManagement/portPolling";
		var data = {
			moid : moid,
			classname : classname,
			devname : devname,
			shelfName : _thisChassis.option.shelf.ShelfName,
			snmp : JSON.stringify(_thisChassis.option.snmp),
			"portType[]" : portType,
			hasPower : hasPower,// 是否有电源口
			slotDif : _thisChassis.option.shelf.SlotDIF
		};
		// console.log(canvasId);
		var successCallBack = function(data) {
			var powerInfo = null;
			if (data.powerInfo)
				powerInfo = data.powerInfo;
			if (data.portInfo) {
				var opt = {
					index : _thisChassis.option.index,
					moid : moid,
					classname : classname,
					devName : devname,
					canvasId : canvasId,
					shelf : _thisChassis.option.shelf,// shelf不变
					cards : _thisChassis.option.cards,// card不变
					ports : data.portInfo,// port为空
					snmp : _thisChassis.option.snmp,// snmp不变
					power : powerInfo
				};
				_initOption(opt);
//				_loadImg();
				_drawShelf();
			}
		}
		$.nmsAjax(url, data, successCallBack);
	}
	function pollingCard() {
		var cardType = [];
		var slotNum = [];
		for ( var i in _thisChassis.option.cards) {
			cardType.push(_thisChassis.option.cards[i].CardType);
			slotNum.push(_thisChassis.option.cards[i].slotNum);
		}
		// alert(id);
		var moid = _thisChassis.option.moid;
		var classname = _thisChassis.option.classname;
		var devname = _thisChassis.option.devName;
		var canvasId = _thisChassis.option.canvasId;
		var url = "topoManagement/cardPolling";
		var data = {
			moid : moid,
			classname : classname,
			devname : devname,
			"cardType[]" : cardType, // 卡的型号
			"cardSlotNum[]" : slotNum,// 卡的槽位号
			snmp : JSON.stringify(_thisChassis.option.snmp),
		};
		// console.log(canvasId);
		var successCallBack = function(data) {
			if (data.cardInfo != null) {// 需要更新则重绘
				var opt = {
					index : _thisChassis.option.index,
					moid : moid,
					classname : classname,
					devName : devname,
					canvasId : canvasId,
					shelf : _thisChassis.option.shelf,// shelf不变
					cards : data.cardInfo,
					ports : [],// port为空
					snmp : _thisChassis.option.snmp,// snmp不变
				};
				_initOption(opt);
//				_loadImg();
				_drawShelf();
			}
		}
		$.nmsAjax(url, data, successCallBack);
	}
	
	//端口开关
	function setPortstatus() {
		if ($("#setStatusDiv_" + _thisChassis.canvasId).length > 0) {
			_thisChassis.portStatusLayerIndex = layer.open({
				type : 1,
				shade : 0.2,
				shadeClose : true,
				title : _thisChassis.portNum,
				area : [ "300px", "140px" ],
				content : $("#setStatusDiv_" + _thisChassis.canvasId),
				zIndex : 999999,
			});
			if (_thisChassis.option.classname == "EponONUDevice"||_thisChassis.option.classname == "GponONUDevice") {
				if (_thisChassis.portStatus == "1"||_thisChassis.portStatus == "2")
					$("#" + _thisChassis.canvasId + "1").prop("checked", 'true');
				else
					$("#" + _thisChassis.canvasId + "2").prop("checked", 'true');
			}else{
				if (_thisChassis.portStatus == "1")
					$("#" + _thisChassis.canvasId + "1").prop("checked", 'true');
				else
					$("#" + _thisChassis.canvasId + "2").prop("checked", 'true');
			}
		} else {
			var setPortstatusDiv = '<div id="setStatusDiv_'
					+ _thisChassis.canvasId
					+ '" style="display:none; position: relative; top: 10px; left: 20px; width: 90%; height: 90%;" >'
					+ '<label style="width: 100px">'
					+ chassisNmsi18n.Portstatus
					+ '</label>'
					+ '<label class="radio-inline">'
					+ '<input type="radio" name="portstatus" id="'
					+ _thisChassis.canvasId
					+ '1" value="1">'
					+ chassisNmsi18n.Up
					+ '</label>'
					+ '<label class="radio-inline">'
					+ '<input type="radio" name="portstatus" id="'
					+ _thisChassis.canvasId
					+ '2" value="2">'
					+ chassisNmsi18n.Down
					+ '</label>'
					+ '<div align="center">'
					+ '<button class="btn btn-info btn-sm" onclick="NMSChassis.savePortStatus('
					+ _thisChassis.option.index
					+ ')">'
					+ chassisNmsi18n.Save
					+ '</button>&nbsp&nbsp&nbsp&nbsp&nbsp'
					+ '<button class="btn btn-sm btn-default" onclick="NMSChassis.closePortStatus('
					+ _thisChassis.option.index + ')">' + chassisNmsi18n.Close
					+ '</button>' + '</div></div>';
			$("body").append(setPortstatusDiv);
			_thisChassis.portStatusLayerIndex = layer.open({
				type : 1,
				shade : 0.2,
				shadeClose : true,
				title : _thisChassis.portNum,
				area : [ "300px", "140px" ],
				content : $("#setStatusDiv_" + _thisChassis.canvasId),
				zIndex : 999999,
			});
			if (_thisChassis.option.classname == "EponONUDevice"||_thisChassis.option.classname == "GponONUDevice") {
				if (_thisChassis.portStatus == "1"||_thisChassis.portStatus == "2")
					$("#" + _thisChassis.canvasId + "1").prop("checked", 'true');
				else
					$("#" + _thisChassis.canvasId + "2").prop("checked", 'true');
			}else{
				if (_thisChassis.portStatus == "1")
					$("#" + _thisChassis.canvasId + "1").prop("checked", 'true');
				else
					$("#" + _thisChassis.canvasId + "2").prop("checked", 'true');
			}
			
		}
	}

	this.savePortStatus = function() {
		var saveStatus = null;
		if ($("#" + _thisChassis.canvasId + "1").prop("checked")) {
			saveStatus = $("#" + _thisChassis.canvasId + "1").val();
		} else {
			saveStatus = $("#" + _thisChassis.canvasId + "2").val();
		}
		nmsShowGlobalLoading();
		if(_thisChassis.option.classname == "EponONUDevice"||_thisChassis.option.classname == "GponONUDevice"){
			var url = "topoManagement/setOnuPortStatus";
			var data = {
				moid:_thisChassis.option.moid,
				classname:_thisChassis.option.classname,
				status:saveStatus,
				portIndex:_thisChassis.portIfIndex,
				snmp:JSON.stringify(_thisChassis.option.snmp)
			};
			var successCallBack = function(data) {
				if(data.result){
					nmsShowSuccessTip(chassisNmsi18n.Set_success);
					pollingPort();
				}
				else{
					nmsShowErrorTip(chassisNmsi18n.Set_failed);
				}
				
				_thisChassis.closePortStatus();
			}
			$.nmsAjax(url, data, successCallBack); 
		}
		else{
			var url = "topoManagement/setPortStatus";
			var data = { 
				status:saveStatus,
				ifIndex:_thisChassis.portIfIndex,
				snmp:JSON.stringify(_thisChassis.option.snmp)
			};
			var successCallBack = function(data) {
				if(data.result){
					nmsShowSuccessTip(chassisNmsi18n.Set_success);
					pollingPort();
				}else{
					nmsShowErrorTip(chassisNmsi18n.Set_failed);
				}
				_thisChassis.closePortStatus();
			}
			$.nmsAjax(url, data, successCallBack); 
		}
	}
	this.closePortStatus = function() {
		layer.close(_thisChassis.portStatusLayerIndex);
	}

	//启用插槽
	function setMenuCardEnable() {
		nmsShowConfirm(function(){
			var url = "topoManagement/setMenuCardEnable";
			var reqData = {
					index:parseInt(_thisChassis.slotNum)*4,
					snmp:JSON.stringify(_thisChassis.option.snmp)
			};
			var successCall = function( data ){
				if(data.result){
					nmsShowSuccessTip(chassisNmsi18n["Slot No Shutdown Succeed"]);
				}else{
					nmsShowErrorTip(chassisNmsi18n["Slot No Shutdown Failed"]);
				}
			}
			$.nmsAjax(url, reqData , successCall);
		},function(){
			parent.layer.close();
			return false;
		},chassisNmsi18n["Are you sure to no shutdown this Slot"]);
	}
	
	//禁用插槽
	function setMenuCardDisable() {
		nmsShowConfirm(function(){
			var url = "topoManagement/setMenuCardDisable";
			var reqData = {
					index:parseInt(_thisChassis.slotNum)*4,
					snmp:JSON.stringify(_thisChassis.option.snmp)
			};
			var successCall = function( data ){
				if(data.result){
					nmsShowSuccessTip(chassisNmsi18n["Slot Shutdown Succeed"]);
				}else{
					nmsShowErrorTip(chassisNmsi18n["Slot Shutdown Failed"]);
				}
			}
			$.nmsAjax(url, reqData , successCall);
		},function(){
			parent.layer.close();
			return false;
		},chassisNmsi18n["Are you sure to shutdown this Slot"]);
	}
	
	//板卡复位
	function resetMenuCard() {
		nmsShowConfirm(function(){
			var url = "topoManagement/resetMenuCard";
			var reqData = {
					index:parseInt(_thisChassis.slotNum)*4,
					snmp:JSON.stringify(_thisChassis.option.snmp)
			};
			var successCall = function( data ){
				if(data.result){
					nmsShowSuccessTip(chassisNmsi18n["Card Reset Succeed"]);
				}else{
					nmsShowErrorTip(chassisNmsi18n["Card Reset Failed"]);
				}
			}
			$.nmsAjax(url, reqData , successCall);
		},function(){
			parent.layer.close();
			return false;
		},chassisNmsi18n["Are you sure to reset this card"]);
	}
	
	//主备切换
	function setCardMasSlvSwitch() {
		nmsShowConfirm(function(){
			var url = "topoManagement/setCardMasSlvSwitch";
			var reqData = {
					index:parseInt(_thisChassis.slotNum)*4,
					snmp:JSON.stringify(_thisChassis.option.snmp)
			};
			var successCall = function( data ){
				if(data.result){
					nmsShowSuccessTip(chassisNmsi18n["Master-Slave Switching Succeed"]);
				}else{
					nmsShowErrorTip(chassisNmsi18n["Master-Slave Switching Failed"]);
				}
			}
			$.nmsAjax(url, reqData , successCall);
		},function(){
			parent.layer.close();
			return false;
		},chassisNmsi18n["Are you sure to Master-Slave Switching"]);
	}
	
	this.initMenu1 = function(tempName) {
		$("#" + _thisChassis.canvasId).smartMenu(
				eval('(' + _thisChassis.nameToMenu[tempName] + ')'), {
					name : "cardMenu",
					textLimit : 15,
					beforeShow : function() {
						// 动态数据，及时清除
						$.smartMenu.remove();
					}
				});
	}
	this.initMenu2 = function() {
		// console.log( _thisChassis.shelfMenu);
		if (_thisChassis.shelfMenu != "")
			$("#" + _thisChassis.canvasId).smartMenu(
					eval('(' + _thisChassis.shelfMenu + ')'), {
						name : "shelfMenu",
						textLimit : 15,
						beforeShow : function() {
							// 动态数据，及时清除
							$.smartMenu.remove();
						}
					});
		else {
			var imageMenuData = [];
			$("#" + _thisChassis.canvasId).smartMenu(imageMenuData, {
				beforeShow : function() {

					// 动态数据，及时清除
					$.smartMenu.remove();
				}
			});
		}
	}
	this.destroy = function(){
		if( _thisChassis._animate ){
			clearInterval(_thisChassis._animate);
		}
		clearInterval(NMSChassis.cardPollingIndex[_thisChassis.option.index]);
		clearInterval(NMSChassis.portPollingIndex[_thisChassis.option.index]);
		_thisChassis._g2d = null;
		_thisChassis._mouseEvent.destoryEvent();
	}
	// 加载参数
	_initOption(opt);
	// 加载画布
	_initCanvas(opt);

	return this;

}

NMSChassis.MEvent = function(canvas, chassis) {

	var thisEvent = this;
	canvas.addEventListener("mousedown", doMouseDown, false); // 摁下
	canvas.addEventListener("mousemove", doMouseMove, false); // 移动
	canvas.addEventListener("mouseup", doMouseUp, false); // 抬起
	canvas.addEventListener("click", doMouseClick, false); // 点击 mouseup之后触发

	this.destoryEvent = function() {
		canvas.removeEventListener("mousedown", doMouseDown, false);
		canvas.removeEventListener("mousemove", doMouseMove, false);
		canvas.removeEventListener("mouseup", doMouseUp, false);
		canvas.removeEventListener("click", doMouseClick, false);

	};

	// 记录上一次鼠标的事件
	var _e = {};
	var showTimer;
	this.mouseDownFlag = false;
	function doMouseDown(e) {
		this.mouseDownFlag = true;
		canvas.style.cursor = "pointer";
		e = e || event;
		var pageTop = document.body.scrollTop;// 页面的偏移y
		var pageLeft = document.body.scrollLeft;// 页面的偏移x
		var layerTop = $("#" + chassis.canvasId + "_layer").parent().offset().top;// 弹窗的偏移y
		var layerLeft = $("#" + chassis.canvasId + "_layer").parent().offset().left;// 弹窗的偏移x
		var layerScrollLeft = $("#" + chassis.canvasId + "_layer").scrollLeft();// 弹窗的滚动条偏移x
		var layerScrollTop = $("#" + chassis.canvasId + "_layer").scrollTop();// 弹窗的滚动条偏移x
		var top = parseInt(layerTop) - parseInt(pageTop)
				- parseInt(layerScrollTop);
		var left = parseInt(layerLeft) - parseInt(pageLeft)
				- parseInt(layerScrollLeft);
		var x = parseInt(e.clientX) - parseInt(left);
		var y = parseInt(e.clientY) - parseInt(top) - 42;

		_e = e;

		chassis.repaintChassisWithEvent(e, x, y);

		chassis.initMenu2();

		if (chassis.cardFlag) {
			var tempName = chassis.cardType + chassis.portType;
			chassis.initMenu1(tempName);
			chassis.cardFlag = false;
			chassis.shelfFlag = false;
			chassis.cardTypeFlag = false;
		}

		else {
			var tempName = chassis.shelfType + "CardMenu";
			if (chassis.shelfFlag) {
				if(chassis.shelfType=="")
					tempName = "blankMenu";
				chassis.initMenu1(tempName);
				// alert("显示shelf菜单...");
				chassis.cardFlag = false;
				chassis.shelfFlag = false;
				chassis.cardTypeFlag = false;
				chassis.shelfType = "";
			}
		}

	}

	function doMouseMove(e) {
		var x = e.clientX - canvas.offsetLeft;
		var y = e.clientY - canvas.offsetTop;

		var _x = _e.clientX - canvas.offsetLeft;
		var _y = _e.clientY - canvas.offsetTop;

		var movex = x - _x;
		var movey = y - _y;
		_e = e;
	}

	function doMouseUp(e) {
		this.mouseDownFlag = false;
		canvas.style.cursor = "";
	}

	function doMouseClick(e) {

		var clickCount = e.detail;

	}

}
NMSChassis.getChassisData = function(displayName,moid, classname, devName, onuName) { // 非onu传moid或name，onu传name
	nmsShowGlobalLoading();
	var url = "topoManagement/getChassis";
	var data = {
		moid : moid,
		classname : classname,
		devname : devName,
		name : onuName
	// devname:"S8510"
	};
	var successCallBack = function(data) {
		if ("Success" == data.chassisInfo.result) {
			var id = NMSChassis.initCanvas();// 生成canvas
			if (onuName != null)
				NMSChassis.openChassisLayer(displayName,data.chassisInfo, id, onuName,
						classname, devName);// 打开弹出层
			else
				NMSChassis.openChassisLayer(displayName,data.chassisInfo, id, moid,
						classname, devName);// 打开弹出层
		} else if ("No Info" == data.chassisInfo.result)
			nmsShowErrorTip(chassisNmsi18n.No_Info);
		else if ("Not Support" == data.chassisInfo.result)
			nmsShowErrorTip(chassisNmsi18n.Not_support);
	}
	$.nmsAjax(url, data, successCallBack);
}
NMSChassis.idToIndex = {};
NMSChassis.initCanvas = function() {
	var id = "chassisCanvas_" + Date.parse(new Date());
	var newChassis = '<canvas id="' + id
			+ '" width="100px" height="100px" ></canvas>';
	$("body").append(newChassis);
	return id;
}
NMSChassis.openChassisLayer = function(displayName,chassisInfo, id, moid, classname,
		devName) {
	
	var index = layer.open({
		id : id + "_layer",
		type : 1,
		shade : 0,
		maxmin : true,
		restore : function(layero) {
			var tmpId = layero[0].children[1].id;
			var tmpIndex = NMSChassis.idToIndex[tmpId];
			NMSChassis.drawRestoreChassis(displayName,chassisInfo, id, moid, classname, devName,
					tmpIndex);
		},
		full : function(layero,index) {
			var tmpId = layero[0].children[1].id;
			var tmpIndex = NMSChassis.idToIndex[tmpId];
			NMSChassis.drawFullChassis(displayName,chassisInfo, id, moid, classname, devName,
					tmpIndex);
		},
//		min : function(layero) {
//		},
		title : "Loading",
		area : [ "100px", "100px" ],
		content : $("#" + id),

		cancel : function(index) {
			NMSChassis.newChassis[index].destroy();
			layer.close(index);
			$("#" + id).remove(); // 关闭后删除对应canvas
			return false;
		},
		zIndex : 99999,

	});
	NMSChassis.idToIndex[id+ "_layer"] = index;
	NMSChassis.drawChassis(displayName,chassisInfo, id, moid, classname, devName, index);
}
NMSChassis.cardPollingIndex = [];// 卡的轮询集合
NMSChassis.portPollingIndex = [];// 端口的轮询集合
NMSChassis.newChassis = [];
NMSChassis.mapWidth = 800;
NMSChassis.mapHeight = 450;
NMSChassis.drawChassis = function(displayName,chassisInfo, id, moid, classname, devName,
		index) {
	var maxWidth = NMSChassis.mapWidth;
	var maxHeight = NMSChassis.mapHeight;
	var shelf_image = new Image();
	shelf_image.src = chassisInfo.shelfInfo.Image.replace("images",
			NMSChassis.BASEURL);
	shelf_image.onload = function() {
		var width = shelf_image.width;
		var height = shelf_image.height;
		if (parseInt(width) > maxWidth) {
			width = maxWidth;
		}

		if (parseInt(height) > maxHeight)
			height = maxHeight;
		var os = NMSChassis.getOs();
		layer.style(index, {
			top : ($(parent.window).height() - parseInt(height)) / 2,
			left : ($(parent.window).width() - parseInt(width)) / 2,
			width : width,
			height : height + 42,
		});
		// console.log(height);
		if (parseInt(shelf_image.width) > maxWidth && os == "Firefox")
			$("#" + id + "_layer").css("height", height + 9 + 13);// 去除火狐y滚动
		else
			$("#" + id + "_layer").css("height", height + 9);
		// layer.title(chassisInfo.shelfTitle, index);
		//layer.title(displayName+" "+chassisInfo.shelfInfo.Title, index);
		layer.title(chassisInfo.shelfInfo.Title, index);
		var option = {
				index : index,
				moid : moid,
				classname : classname,
				devName : devName,
				canvasId : id,
				shelf : chassisInfo.shelfInfo,
				cards : chassisInfo.cardInfo,
				ports : [],
				snmp : chassisInfo.snmpInfo,
				canvasWidth : shelf_image.width,
				canvasHeight : shelf_image.height

			};
		NMSChassis.newChassis[index] = new NMSChassis.shelfFrame(option);
		NMSChassis.newChassis[index].initAndShow();

	}
}
NMSChassis.drawFullChassis = function(displayName,chassisInfo,id,moid, classname, devName,index){
	var shelf_image = new Image();
	shelf_image.src = chassisInfo.shelfInfo.Image.replace("images",
			NMSChassis.BASEURL);
	shelf_image.onload = function() {
		$("#" + id + "_layer").css("height", $(parent.window).height());
		layer.title(displayName+" "+chassisInfo.shelfInfo.Title, index);
		var option = {
				index : index,
				moid : moid,
				classname : classname,
				devName : devName,
				canvasId : id,
				shelf : chassisInfo.shelfInfo,
				cards : chassisInfo.cardInfo,
				ports : [],
				snmp : chassisInfo.snmpInfo,
				canvasWidth : shelf_image.width,
				canvasHeight : shelf_image.height,

			};
//		NMSChassis.newChassis[index] = new NMSChassis.shelfFrame(option);
		NMSChassis.newChassis[index].updateOption(shelf_image.width,shelf_image.height);

	}
}
NMSChassis.drawRestoreChassis = function(displayName,chassisInfo,id,moid, classname, devName,index){
	var maxWidth = NMSChassis.mapWidth;
	var maxHeight = NMSChassis.mapHeight;
	var shelf_image = new Image();
	shelf_image.src = chassisInfo.shelfInfo.Image.replace("images",
			NMSChassis.BASEURL);
	shelf_image.onload = function() {
		var width = shelf_image.width;
		var height = shelf_image.height;
		if (parseInt(width) > maxWidth) {
			width = maxWidth;
		}

		if (parseInt(height) > maxHeight)
			height = maxHeight;
		var os = NMSChassis.getOs();
		layer.style(index, {
			top : ($(parent.window).height() - parseInt(height)) / 2,
			left : ($(parent.window).width() - parseInt(width)) / 2,
			width : width,
			height : height + 42,
		});
		// console.log(height);
		if (parseInt(shelf_image.width) > maxWidth && os == "Firefox")
			$("#" + id + "_layer").css("height", height + 9 + 13);// 去除火狐y滚动
		else
			$("#" + id + "_layer").css("height", height + 9);
		layer.title(displayName+" "+chassisInfo.shelfInfo.Title, index);
		var option = {
				index : index,
				moid : moid,
				classname : classname,
				devName : devName,
				canvasId : id,
				shelf : chassisInfo.shelfInfo,
				cards : chassisInfo.cardInfo,
				ports : [],
				snmp : chassisInfo.snmpInfo,
				canvasWidth : shelf_image.width,
				canvasHeight : shelf_image.height,

			};
//		NMSChassis.newChassis[index] = new NMSChassis.shelfFrame(option);
		NMSChassis.newChassis[index].updateOption( shelf_image.width,shelf_image.height);

	}
}
NMSChassis.savePortStatus = function(index) {
	NMSChassis.newChassis[index].savePortStatus();
}
NMSChassis.closePortStatus = function(index) {
	NMSChassis.newChassis[index].closePortStatus();
}

NMSChassis.getOs = function() {
	var OsObject = "";
	if (navigator.userAgent.indexOf("MSIE") > 0) {
		return "MSIE";
	}
	if (isFirefox = navigator.userAgent.indexOf("Firefox") > 0) {
		return "Firefox";
	}
	if (isSafari = navigator.userAgent.indexOf("Safari") > 0) {
		return "Safari";
	}
	if (isCamino = navigator.userAgent.indexOf("Camino") > 0) {
		return "Camino";
	}
	if (isMozilla = navigator.userAgent.indexOf("Gecko/") > 0) {
		return "Gecko";
	}

}
